package com.loop.unclelend.utils.event

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/3 10:52
 * @Annotation：发布者
 */
class UnclePublisher {

    private var subscriberList: ArrayList<UncleSubscriber>? = null
    init {
        subscriberList = arrayListOf()
    }

    /**
     * 注册订阅者
     */
    fun register(subscriber: UncleSubscriber){
        var isAdd = true
        for (i in 0..<subscriberList!!.size){
            if (subscriberList!![i] == subscriber){
                isAdd = false
                break
            }
        }
        if (isAdd){
            subscriberList!!.add(subscriber)
        }
    }

    /**
     * 发布信息
     */
    fun publish(event: UncleEventData){
        subscriberList!!.forEach {
            it.onUncleEvent(event)
        }
    }

    /**
     * 移除订阅者
     */
    fun detach(subscriber: UncleSubscriber){
        subscriberList!!.remove(subscriber)
    }
}